/**
 * FrameLooper
 *
 * @author		Allen Chou
 * @version		1.0.0 (last update: Aug 5 2008)
 * @link		http://cjcat2266.deviantart.com
 */

package idv.cjcat.utils.framelooper {
	import flash.display.MovieClip;
	import flash.errors.IllegalOperationError;
	
	import idv.cjcat.utils.StepNum;
	
	public class FrameLooper {
		
		private var _stepper:StepNum;
		private var _obj:MovieClip;
		
		public function FrameLooper(obj:MovieClip) {
			this.setObj(obj);
		}
		
		public function goto(value:Number, gotoAndPlay:Boolean = false):void {
			_stepper.setValue(value);
			if (!gotoAndPlay) {
				_obj.gotoAndStop(Math.floor(_stepper.getValue() + 1));
			} else {
				_obj.gotoAndPlay(Math.ceil(_stepper.getValue() + 1));
			}
		}
		
		public function step(value:Number = 1, gotoAndPlay:Boolean = false):void {
			_stepper.step(value);
			goto(_stepper.getValue());
		}
		
		public function getFrame():Number {
			return _obj.currentFrame;
		}
		
		public function getObj():MovieClip { return _obj; }
		public function setObj(obj:MovieClip, initFrame:Number = 1, gotoAndPlay:Boolean = false):void {
			if (obj == null) throw new IllegalOperationError("The obj property must not be null.");
			_obj = obj;
			_stepper = new StepNum(0, _obj.totalFrames - 1, initFrame);
			
			goto(initFrame);
		}
	}
}